<template>
<div class="login">
    <el-form  :model="ruleForm" status-icon label-width="80px" class="form-position" :rules="rules" ref="validataform">
        <h1 class="logo">
            <img src="../assets/谭艳.png" alt="">
        </h1>
        <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入用户名" ></el-input>
        </el-form-item>
            <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
           <el-button type="primary" @click="submitForm('ruleForm')" class="loginBtn">登录</el-button>
           <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</template>

<script>
// import axios from 'axios'
  export default {
      data() {
          return {
            ruleForm: {
              username: '',
              password: ''
            },
            rules: {
              username: [
                { required: true, message: '请输入用户名', trigger: ['blur' ,'change']},
                { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: ['blur','change'] }
              ],
              password: [
                { required: true, message: '请输入密码', trigger: ['blur' ,'change'] },
                { min: 3, max: 9, message: '长度在 6 到 12个字符', trigger: ['blur' ,'change'] }
              ],
            }
          }
        },
      methods:{
            submitForm(ruleForm) {
                this.$refs.validataform.validate((valid) => {
                    if(!valid){
                        this.$message({
                            message: '请认真核对信息',
                            type: 'error'
                        });
                        return false;
                    }
                this.$axios({
                    method:"post",
                    url:'login',
                    data:this.ruleForm
                  }).then(res=>{
                        if(res.data.meta.status==200){
                            this.$message({
                              message:res.data.meta.msg ,
                              type: 'success'
                          });
                          localStorage.setItem("token",res.data.data.token);
                          this.$router.push({path:"/index"});
                        }else if(res.data.meta.status==400){
                              this.$message({
                                  message:res.data.meta.msg ,
                                  type: 'error'
                              });
                          }
                        })   
                   });
            },
          resetForm(ruleForm) {
            this.$refs.validataform.resetFields();
          }
        }
  }
</script>

<style lang="less">
.login{
    width: 100%;
    height: 100%;
    background: #2d434c;
    overflow: hidden;
    .form-position{
        margin-top:100px;
        margin-left:auto;
        margin-right:auto;
        padding-right:20px;
        padding-top:70px;
        width: 300px;
        background: white;
        padding-bottom: 20px;
        border-radius:20px;
        position: relative;
        .loginBtn{
            margin-right: 40px;
        }
        .logo{
            position:absolute;
            left:50%;
            top:-50px;
            width: 100px;
            height: 100px;
            margin-left:-50px;
            border-radius:50%;
            box-shadow: 0 0 5px white;
            border:5px solid white;
            img{
                width: 100%;
                height: 100%;
                border-radius:50%;
                box-shadow: 0 0 5px white;
            }
        }
    }

}
</style>
